home *** CD-ROM | disk | FTP | other *** search
- /* CMyDocument.c */
-
- #define compilingmydocument_c
-
- #include "CMyDocument.h"
- #include "MenuController.h"
- #include "CSaveBeforeClosing.h"
- #include "Memory.h"
- #include "CApplication.h"
- #include "File.h"
- #include "CSack.h"
- #include "Alert.h"
- #include "CArray.h"
- #include "PortableFile.h"
- #include "Error.h"
- #include "StandardFile.h"
- #include <Sound.h>
- #include "CSongList.h"
- #include "CRewindButton.h"
- #include "CFastForwardButton.h"
- #include "CStopButton.h"
- #include "CPlayButton.h"
- #include "CPauseButton.h"
- #include "CSkipToNextButton.h"
- #include "LocationConstants.h"
- #include "CStaticText.h"
- #include "StringUtils.h"
- #include "CCheckbox.h"
- #include "CNumberEdit.h"
- #include "CDefaultStereoOn.h"
- #include "CDefaultAntiAlias.h"
- #include "CDefaultSamplingRate.h"
- #include "CDefaultStereoMix.h"
- #include "CDefaultNumRepeats.h"
- #include "CDefaultSpeed.h"
- #include "CSpecificStereoOn.h"
- #include "CSpecificAntiAlias.h"
- #include "CSpecificSamplingRate.h"
- #include "CSpecificStereoMix.h"
- #include "CSpecificNumRepeats.h"
- #include "CSpecificSpeed.h"
- #include "COverrideStereoOn.h"
- #include "COverrideAntiAlias.h"
- #include "COverrideSamplingRate.h"
- #include "COverrideStereoMix.h"
- #include "COverrideNumRepeats.h"
- #include "COverrideSpeed.h"
- #include "CDividerLine.h"
- #include "CMyApplication.h"
- #include "CSpecificVolume.h"
- #include "CDefaultVolume.h"
- #include "COverrideVolume.h"
- #include "CAutoNextSong.h"
- #include "CRepeat.h"
- #include "CAutoStartSongs.h"
- #include "CRandomize.h"
- #include "CNumOutputBits.h"
- #include "Compatibility.h"
-
-
- #define UntitledStringID (6592L*65536L + 1)
- #define SeparatorID (6592L*65536L + 2)
- #define EVENTDELAY (15)
-
- #define CouldntSaveSongID (5120L*65536L + 1)
- #define ErrorOccurredWhileLoadingID (5120L*65536L + 2)
-
- #define MAXRANDOMTRIESBEFOREGIVINGUP (50)
-
-
- /* */ CMyDocument::CMyDocument()
- {
- CArray* SongTemp;
- Handle Temp;
-
- SongTemp = new CArray;
- SongTemp->IArray(sizeof(SongRec),16);
- ListOfSongs = SongTemp;
-
- RootWindow = NIL;
- EverSaved = False;
- UpToDate = True;
- FileOpenFlag = False;
-
- SamplingRate = 22254;
- StereoOn = False;
- StereoMix = 0;
- AntiAliasing = True;
- Speed = 50;
- NumRepeats = 1;
- Volume = 48;
- AutoNextSong = True;
- Repeat = False;
- AutoStartSongs = True;
- Randomize = False;
- UseSixteenBitMode = False;
-
- Selection = -1;
-
- SongToStart = -1;
- PlayerExists = False;
- PlayNextWhenThisOneStops = False;
- Playing = -1;
-
- GetDateTime((void*)&randSeed);
-
- Temp = GetCString(UntitledStringID);
- Handle2PString(Temp,TheFile.name);
- ReleaseHandle(Temp);
- }
-
-
- /* */ CMyDocument::~CMyDocument()
- {
- long Scan;
- SongRec Thang;
-
- DeregisterIdler(this);
-
- if (RootWindow != NIL)
- {
- delete RootWindow;
- }
-
- for (Scan = 0; Scan < ListOfSongs->GetNumElements(); Scan += 1)
- {
- ListOfSongs->GetElement(Scan,&Thang);
- ReleaseHandle((Handle)Thang.SongLocation);
- }
- delete ListOfSongs;
-
- if (FileOpenFlag)
- {
- FCloseFile(FileRef);
- }
- }
-
-
- void CMyDocument::DoNewFile(void)
- {
- MakeNewWindow();
- }
-
-
- MyBoolean CMyDocument::GoAway(void)
- {
- if (!UpToDate && EverSaved)
- {
- CSaveBeforeClosingWindow* Saver;
- MyBoolean Result;
-
- Saver = new CSaveBeforeClosingWindow;
- HLock((Handle)this);
- Result = Saver->SaveBeforeClosing(TheFile.name);
- HUnlock((Handle)this);
- switch (Result)
- {
- case Yes_Save:
- if (!SaveFile())
- {
- return False;
- }
- break;
- case No_Save:
- break;
- case Cancel_Close:
- return False;
- }
- }
-
- if (PlayerExists)
- {
- Application->KillPlayer(OurPlayer);
- }
-
- delete this;
- return True;
- }
-
-
- void CMyDocument::DoPrint(void)
- {
- }
-
-
- void CMyDocument::DoOpenFile(FSSpec* TheSpec)
- {
- short LocalRef;
- ulong SongCount;
-
- /* reading in all them alias records */
- TheFile = *TheSpec;
- if (FOpenFile(TheSpec,&LocalRef) != noErr)
- {
- /* error */
- }
- FileOpenFlag = True;
- FileRef = LocalRef;
-
- ResetErrorCheck();
- SamplingRate = (ushort)RShort(LocalRef);
- StereoOn = RChar(LocalRef);
- StereoMix = RShort(LocalRef);
- AntiAliasing = RChar(LocalRef);
- Speed = RShort(LocalRef);
- NumRepeats = RShort(LocalRef);
- Volume = RShort(LocalRef);
- AutoNextSong = RChar(LocalRef);
- Repeat = RChar(LocalRef);
- AutoStartSongs = RChar(LocalRef);
- Randomize = RChar(LocalRef);
-
- SongCount = RShort(LocalRef);
- while (SongCount > 0)
- {
- ulong AliasLength;
- SongRec DaAlias;
- long NewEntry;
-
- DaAlias.SamplingRateOverrideDefault = RChar(LocalRef);
- DaAlias.SamplingRate = (ushort)RShort(LocalRef);
- DaAlias.StereoOnOverrideDefault = RChar(LocalRef);
- DaAlias.StereoOn = RShort(LocalRef);
- DaAlias.StereoMixOverrideDefault = RChar(LocalRef);
- DaAlias.StereoMix = RShort(LocalRef);
- DaAlias.AntiAliasingOverrideDefault = RChar(LocalRef);
- DaAlias.AntiAliasing = RShort(LocalRef);
- DaAlias.SpeedOverrideDefault = RChar(LocalRef);
- DaAlias.Speed = RShort(LocalRef);
- DaAlias.NumRepeatsOverrideDefault = RChar(LocalRef);
- DaAlias.NumRepeats = RShort(LocalRef);
- DaAlias.VolumeOverrideDefault = RChar(LocalRef);
- DaAlias.Volume = RShort(LocalRef);
- DaAlias.Tracker = RChar(LocalRef);
- FReadBlock(LocalRef,(char*)DaAlias.SongName,SongNameLength);
- AliasLength = RLong(LocalRef);
- DaAlias.SongLocation = (AliasRecord**)AllocHandle(AliasLength);
- HLock((Handle)DaAlias.SongLocation);
- FReadBlock(LocalRef,(char*)*DaAlias.SongLocation,AliasLength);
- HUnlock((Handle)DaAlias.SongLocation);
- DaAlias.PlayedFlag = False;
- NewEntry = ListOfSongs->AppendElement();
- ListOfSongs->PutElement(NewEntry,&DaAlias);
- SongCount -= 1;
- }
- if (ErrorOccurred())
- {
- AlertError(ErrorOccurredWhileLoadingID,NIL);
- }
- MakeNewWindow();
- SongList->RecalculateScrollBars();
- if ((LastModifiers & controlKey) == 0)
- {
- /* if they were NOT holding down the control key, then we want */
- /* to see if they are now. Otherwise, we don't want to clear it. */
- RelinquishCPU();
- }
- if (AutoStartSongs && ((LastModifiers & controlKey) == 0))
- {
- /* if they want to autostart songs upon opening the document AND */
- /* they didn't hold down the command key while selecting "open" */
- /* (or starting the program) then we can do it. */
- if (Randomize)
- {
- StartRandomSong();
- }
- else
- {
- StartThisSong(0);
- }
- }
- EverSaved = True;
- }
-
-
- MyBoolean CMyDocument::SaveFile(void)
- {
- if (EverSaved)
- {
- WriteData();
- return True;
- }
- else
- {
- return SaveFileAs();
- }
- }
-
-
- MyBoolean CMyDocument::SaveFileAs(void)
- {
- FSSpec NewInfo;
- MyBoolean Result;
- MyBoolean Replacing;
-
- HLock((Handle)this);
- Result = FPutFile(TheFile.name,&NewInfo,&Replacing);
- if (Result)
- {
- if (Replacing)
- {
- FDeleteFile(&NewInfo);
- }
- TheFile = NewInfo;
- FCreate(&NewInfo,CREATORCODE,FILETYPE1);
- FOpenFile(&NewInfo,&FileRef);
- FileOpenFlag = True;
- WriteData();
- EverSaved = True;
- SetWTitle(RootWindow->MyGrafPtr,TheFile.name);
- }
- HUnlock((Handle)this);
- return Result;
- }
-
-
- void CMyDocument::WriteData(void)
- {
- ulong SongCount;
- ulong SongScan;
- long FilePosTemp;
-
- ERROR(!FileOpenFlag,PRERR(ForceAbort,"CMyDocument::WriteData called when "
- "no file was open for writing."));
-
- SpecificSamplingRateBox->StoreValue();
- SpecificStereoMixBox->StoreValue();
- SpecificNumRepeatsBox->StoreValue();
- SpecificSpeedBox->StoreValue();
- SpecificVolumeBox->StoreValue();
- DefaultSamplingRateBox->StoreValue();
- DefaultStereoMixBox->StoreValue();
- DefaultNumRepeatsBox->StoreValue();
- DefaultSpeedBox->StoreValue();
- DefaultVolumeBox->StoreValue();
-
- ResetErrorCheck();
- FSetFilePos(FileRef,0);
-
- WShort(FileRef,SamplingRate);
- WChar(FileRef,StereoOn);
- WShort(FileRef,StereoMix);
- WChar(FileRef,AntiAliasing);
- WShort(FileRef,Speed);
- WShort(FileRef,NumRepeats);
- WShort(FileRef,Volume);
- WChar(FileRef,AutoNextSong);
- WChar(FileRef,Repeat);
- WChar(FileRef,AutoStartSongs);
- WChar(FileRef,Randomize);
-
- SongCount = ListOfSongs->GetNumElements();
- WShort(FileRef,SongCount);
- for (SongScan = 0; SongScan < SongCount; SongScan += 1)
- {
- SongRec Song;
- ulong AliasSize;
-
- ListOfSongs->GetElement(SongScan,&Song);
- WChar(FileRef,Song.SamplingRateOverrideDefault);
- WShort(FileRef,Song.SamplingRate);
- WChar(FileRef,Song.StereoOnOverrideDefault);
- WShort(FileRef,Song.StereoOn);
- WChar(FileRef,Song.StereoMixOverrideDefault);
- WShort(FileRef,Song.StereoMix);
- WChar(FileRef,Song.AntiAliasingOverrideDefault);
- WShort(FileRef,Song.AntiAliasing);
- WChar(FileRef,Song.SpeedOverrideDefault);
- WShort(FileRef,Song.Speed);
- WChar(FileRef,Song.NumRepeatsOverrideDefault);
- WShort(FileRef,Song.NumRepeats);
- WChar(FileRef,Song.VolumeOverrideDefault);
- WShort(FileRef,Song.Volume);
- WChar(FileRef,Song.Tracker);
- FWriteBlock(FileRef,(char*)Song.SongName,SongNameLength);
- AliasSize = HandleSize((Handle)Song.SongLocation);
- WLong(FileRef,AliasSize);
- HLock((Handle)Song.SongLocation);
- FWriteBlock(FileRef,(char*)*Song.SongLocation,AliasSize);
- HUnlock((Handle)Song.SongLocation);
- }
- FGetFilePos(FileRef,&FilePosTemp);
- FSetEOF(FileRef,FilePosTemp);
- if (ErrorOccurred())
- {
- AlertError(CouldntSaveSongID,NIL);
- }
- else
- {
- UpToDate = True;
- }
- }
-
-
- void CMyDocument::AddSong(FSSpec* TheSpec)
- {
- AliasRecord** SystemAlias;
- SongRec MyAlias;
- long IndexToStoreAt;
- OSErr Error;
- short Scan;
-
- for (Scan = 0; Scan < sizeof(MyAlias); Scan += 1)
- {
- ((char*)&MyAlias)[Scan] = 0;
- }
- MyAlias.SamplingRateOverrideDefault = False;
- MyAlias.StereoOnOverrideDefault = False;
- MyAlias.StereoMixOverrideDefault = False;
- MyAlias.AntiAliasingOverrideDefault = False;
- MyAlias.SpeedOverrideDefault = False;
- MyAlias.NumRepeatsOverrideDefault = False;
- MyAlias.VolumeOverrideDefault = False;
- MyAlias.SamplingRate = SamplingRate;
- MyAlias.StereoOn = StereoOn;
- MyAlias.StereoMix = StereoMix;
- MyAlias.AntiAliasing = AntiAliasing;
- MyAlias.Speed = Speed;
- MyAlias.NumRepeats = NumRepeats;
- MyAlias.Volume = Volume;
- MyAlias.Tracker = False;
- MemCpy((char*)MyAlias.SongName,(char*)TheSpec->name,TheSpec->name[0] + 1);
- if (MyAlias.SongName[0] > SongNameLength - 1)
- {
- MyAlias.SongName[0] = SongNameLength - 1;
- }
- Error = NewAlias(NIL,TheSpec,&SystemAlias);
- MyAlias.SongLocation = (AliasRecord**)DupSysHandle((Handle)SystemAlias);
- DisposHandle((Handle)SystemAlias);
- MyAlias.PlayedFlag = False;
- IndexToStoreAt = ListOfSongs->AppendElement();
- ListOfSongs->PutElement(IndexToStoreAt,&MyAlias);
- EXECUTE(ListOfSongs->GetElement(IndexToStoreAt,&MyAlias);)
- SongList->Redraw(IndexToStoreAt,IndexToStoreAt);
- SongList->RecalculateScrollBars();
- if ((LastModifiers & controlKey) == 0)
- {
- /* if they were NOT holding down the control key, then we want */
- /* to see if they are now. Otherwise, we don't want to clear it. */
- RelinquishCPU();
- }
- if ((Playing == -1) && (SongToStart == -1) && AutoStartSongs
- && ((LastModifiers & controlKey) == 0))
- {
- StartThisSong(IndexToStoreAt);
- }
- UpToDate = False;
- }
-
-
- void CMyDocument::CancelCurrentSong(void)
- {
- if (PlayerExists)
- {
- Application->KillPlayer(OurPlayer);
- PlayerState = PlayerDying;
- }
- PlayNextWhenThisOneStops = False;
- }
-
-
- void CMyDocument::StartThisSong(long SongIndex)
- {
- SongRec Temp;
-
- if (Playing != -1)
- {
- CancelCurrentSong();
- }
- if ((SongIndex >= 0) && (SongIndex < ListOfSongs->GetNumElements()))
- {
- RegisterIdler(this,EVENTDELAY);
- SongToStart = SongIndex;
- ListOfSongs->GetElement(SongIndex,&Temp);
- Temp.PlayedFlag = True;
- ListOfSongs->PutElement(SongIndex,&Temp);
- }
- PlayNextWhenThisOneStops = AutoNextSong;
- }
-
-
- void CMyDocument::RemoveSongFromList(long SongIndex)
- {
- SongRec Temp;
-
- if ((SongIndex >= 0) && (SongIndex < ListOfSongs->GetNumElements()))
- {
- if (SongIndex == Playing)
- {
- CancelCurrentSong();
- }
- if (SongIndex < Playing)
- {
- Playing -= 1;
- }
- UpToDate = False;
- ListOfSongs->GetElement(SongIndex,&Temp);
- ReleaseHandle((Handle)Temp.SongLocation);
- ListOfSongs->DeleteElement(SongIndex);
- SongList->Redraw(SongIndex,ListOfSongs->GetNumElements());
- }
- }
-
-
- void CMyDocument::MakeNewWindow(void)
- {
- CWindow* Window;
- LongPoint Start;
- LongPoint Extent;
- CStaticText* StaticText;
- CPlayButton* PlayButton;
- CDividerLine* DividerLine;
-
-
- MoveHHi((Handle)this);
- HLock((Handle)this);
-
-
- Window = new CWindow;
- GetRect(WindowLocsID,&Start,&Extent);
- Start = CenterRect(Extent,MainScreenSize());
- Window->IWindow(Start,Extent,ModelessWindow,NoGrowable,NoZoomable);
- RootWindow = Window;
- SetWTitle(Window->MyGrafPtr,TheFile.name);
-
-
- SongList = new CSongList;
- SongList->ISongList(this,Window);
-
-
- RewindButton = new CRewindButton;
- RewindButton->IRewindButton(this,Window);
-
- StopButton = new CStopButton;
- StopButton->IStopButton(this,Window);
-
- PlayButton = new CPlayButton;
- PlayButton->IPlayButton(this,Window);
- PausePlayButton = PlayButton;
-
- FastForwardButton = new CFastForwardButton;
- FastForwardButton->IFastForwardButton(this,Window);
-
- SkipToNextButton = new CSkipToNextButton;
- SkipToNextButton->ISkipToNextButton(this,Window);
-
-
- StaticText = new CStaticText;
- GetRect(DefaultSettingsID,&Start,&Extent);
- StaticText->IStaticText(Start,Extent,GetCString(DefaultSettingsID),
- applFont,9,Window,Window,JustifyLeft);
-
- StaticText = new CStaticText;
- GetRect(DefaultFrequencyID,&Start,&Extent);
- StaticText->IStaticText(Start,Extent,GetCString(DefaultFrequencyID),
- applFont,9,Window,Window,JustifyLeft);
-
- StaticText = new CStaticText;
- GetRect(DefaultSpeedID,&Start,&Extent);
- StaticText->IStaticText(Start,Extent,GetCString(DefaultSpeedID),
- applFont,9,Window,Window,JustifyLeft);
-
- StaticText = new CStaticText;
- GetRect(DefaultRepeatsID,&Start,&Extent);
- StaticText->IStaticText(Start,Extent,GetCString(DefaultRepeatsID),
- applFont,9,Window,Window,JustifyLeft);
-
- StaticText = new CStaticText;
- GetRect(DefaultMixID,&Start,&Extent);
- StaticText->IStaticText(Start,Extent,GetCString(DefaultMixID),
- applFont,9,Window,Window,JustifyLeft);
-
- StaticText = new CStaticText;
- GetRect(DefaultVolumeID,&Start,&Extent);
- StaticText->IStaticText(Start,Extent,GetCString(DefaultVolumeID),
- applFont,9,Window,Window,JustifyLeft);
-
- SpecificSamplingRateText = new CStaticText;
- GetRect(SpecificFrequencyID,&Start,&Extent);
- SpecificSamplingRateText->IStaticText(Start,Extent,GetCString(SpecificFrequencyID),
- applFont,9,Window,Window,JustifyLeft);
-
- SpecificSpeedText = new CStaticText;
- GetRect(SpecificSpeedID,&Start,&Extent);
- SpecificSpeedText->IStaticText(Start,Extent,GetCString(SpecificSpeedID),
- applFont,9,Window,Window,JustifyLeft);
-
- SpecificNumRepeatsText = new CStaticText;
- GetRect(SpecificRepeatsID,&Start,&Extent);
- SpecificNumRepeatsText->IStaticText(Start,Extent,GetCString(SpecificRepeatsID),
- applFont,9,Window,Window,JustifyLeft);
-
- SpecificStereoMixText = new CStaticText;
- GetRect(SpecificMixID,&Start,&Extent);
- SpecificStereoMixText->IStaticText(Start,Extent,GetCString(SpecificMixID),
- applFont,9,Window,Window,JustifyLeft);
-
- SpecificVolumeText = new CStaticText;
- GetRect(SpecificVolumeID,&Start,&Extent);
- SpecificVolumeText->IStaticText(Start,Extent,GetCString(SpecificVolumeID),
- applFont,9,Window,Window,JustifyLeft);
-
- AutoNextSongBox = new CAutoNextSong;
- AutoNextSongBox->IAutoNextSong(this,Window);
-
- RepeatBox = new CRepeat;
- RepeatBox->IRepeat(this,Window);
-
- AutoStartSongsBox = new CAutoStartSongs;
- AutoStartSongsBox->IAutoStartSongs(this,Window);
-
- RandomizeBox = new CRandomize;
- RandomizeBox->IRandomize(this,Window);
-
- DividerLine = new CDividerLine;
- DividerLine->IDividerLine(Window);
-
-
- DefaultStereoOnBox = new CDefaultStereoOn;
- DefaultStereoOnBox->IDefaultStereoOn(this,Window);
-
- DefaultAntiAliasingBox = new CDefaultAntiAliasing;
- DefaultAntiAliasingBox->IDefaultAntiAliasing(this,Window);
-
- DefaultVolumeBox = new CDefaultVolume;
- DefaultVolumeBox->IDefaultVolume(this,Window);
- DefaultVolumeBox->SetValue(Volume);
-
- DefaultSamplingRateBox = new CDefaultSamplingRate;
- DefaultSamplingRateBox->IDefaultSamplingRate(this,Window);
- DefaultSamplingRateBox->SetValue(SamplingRate);
-
- DefaultSpeedBox = new CDefaultSpeed;
- DefaultSpeedBox->IDefaultSpeed(this,Window);
- DefaultSpeedBox->SetValue(Speed);
-
- DefaultNumRepeatsBox = new CDefaultNumRepeats;
- DefaultNumRepeatsBox->IDefaultNumRepeats(this,Window);
- DefaultNumRepeatsBox->SetValue(NumRepeats);
-
- DefaultStereoMixBox = new CDefaultStereoMix;
- DefaultStereoMixBox->IDefaultStereoMix(this,Window);